import isg.etsii.ull.es.DAO.UsuarioRegistradoDAO;
import isg.etsii.ull.es.UsuarioRegistrado;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Servlet implementation class MiServlet
 */
@SuppressWarnings("unused")
public class ServletAutenticarse extends HttpServlet {
	private static final long serialVersionUID = 1L;	private UsuarioRegistradoDAO URDAO = null;

    @Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		
		try {
			URDAO = new UsuarioRegistradoDAO();
		} catch (ClassNotFoundException e) {
			throw new ServletException(e);
		} catch (SQLException e) {
			throw new ServletException(e);
		}	

    }
   
    public ServletAutenticarse() {
        super();
        // TODO Auto-generated constructor stub
    }
 
  	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		// TODO Auto-generated method stub
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
			dispatcher.forward(req,res);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try {
			Autenticarse(req,res);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	protected void Autenticarse(HttpServletRequest req, HttpServletResponse res) throws IOException, ClassNotFoundException, SQLException, ServletException {
		UsuarioRegistradoDAO usrdao = new UsuarioRegistradoDAO();
		UsuarioRegistrado usr = new UsuarioRegistrado();
		String contrasena = req.getParameter("cont");
		String nombre = req.getParameter("nombre");
		usr.setNUsuario(nombre);
		usr.setClave(contrasena);
		
	    int existeclave = usrdao.existeClave(nombre, contrasena);
		if (existeclave == 1) {
     		HttpSession session = req.getSession(true);
				
			String username = usr.getNUsuario();
			String first = "si";
			int error = 0;
			String salir = "No";
				
	        session.setAttribute("username", username);
	        session.setAttribute("first", first);
	        session.setAttribute("error", error);
	        session.setAttribute("salir", salir);
		        
	        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Autenticado.jsp");
	   		dispatcher.forward(req,res);
		} // if
		else {
			int error = 2;
			
			HttpSession session = req.getSession(true);
			session.setAttribute("error", error);
	        
	        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/error.jsp");
	   		dispatcher.forward(req,res);
		}
		
	} // void
}
